import nltk 

def get_sentences(directory, polarity):
    """Given a directory to look in and the polarity we want, return a tokenized
    list of the sentences in one of our data sets.
    """
    path = '../data/' + directory
    if polarity == 'p':
        path = path + '/positives.txt'
    else:
        path = path + '/negatives.txt'
    fr = open(path, 'r')
    sentences = [nltk.word_tokenize(line.rstrip('.\n')) for line in 
                 fr.readlines()]
   
    return sentences

def corpus(positive_sentences, negative_sentences):
    """Create a corpus as closure.
    """
    def dispatch(t):
        if t == 'p_sentences':
            return positive_sentences
        elif t == 'n_sentences':
            return negative_sentences
        elif t == 'sentences':
            return positive_sentences + negative_sentences
        elif t == 'p_words':
            return [word for sentence in positive_sentences for word in
                    sentence]
        elif t == 'n_words':
            return [word for sentence in negative_sentences for word in
                    sentence]
        elif t == 'words':
            return [word for sentence in (negative_sentences +
                                          positive_sentences) for word in
                    sentence]
        else:
            print 'Invalid input\n'
    
    return dispatch

def build_corpus(directory):
    """Given the directory of one of our data sets, return a corpus containing
    the data.
    """
    path = '../data/' + directory
    positive_sentences = get_sentences(directory, 'p')
    negative_sentences = get_sentences(directory, 'n')
    
    return corpus(positive_sentences, negative_sentences)

def concatenate(*lists):
    """Concatenate a variable number of lists.
    """
    return [item for sublist in lists for item in sublist]
        
def merge_corpora(*corpora):
    """Merge the features of multiple corpora together.
    """
    positive_sentences = tuple([c('p_sentences') for c in corpora])
    negative_sentences = tuple([c('n_sentences') for c in corpora])
    positive_sentences = map(concatenate, *positive_sentences)
    negative_sentences = map(concatenate, *negative_sentences)
       
    return corpus(positive_sentences, negative_sentences)
